package li.g.zhu.ExecutorService.work;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestExecutorThreadPool {
	public static void threadName(int num) {
		int tagListSize = 10;
		int latchSize = (num / tagListSize);
		if ((num % tagListSize) == 0) {
			latchSize++;
		}
		System.out.println("******* latchSize " + latchSize + " ****");
		int poolSize = (latchSize / 2) + 1;
		System.out.println("******* poolSize " + poolSize + " ****");
		ExecutorService executorService = Executors.newFixedThreadPool(poolSize);
		CountDownLatch latch = new CountDownLatch(latchSize);
		for (int i = 0; i < num; i++) {
			executorService.execute(new TestRunnable(latch , i));
			System.out.println("******* deal i " + i + " ****");
		}
		System.out.println("******* for is over  " );
		try {
			latch.await();
			System.out.println("******* await is over  " );
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		executorService.shutdown();
	}

}

class TestRunnable implements Runnable {
	private CountDownLatch latch;
	private int num;

	public TestRunnable(CountDownLatch latch, int num) {
		this.latch = latch;
		this.num = num;
	}

	public void run() {
		try {
			String currentThread = Thread.currentThread().getName();
			System.out.println(currentThread + "线程被调用了" + " latch size :" + latch.getCount()+" num : "+num);
			Thread.sleep(10000);
			latch.countDown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
